<?php
class commentController extends Controller{
    public static function _right(){
        return array(
            array('广告管理'),
            array('广告添加','add,add_'),
            array('广告列表','mylist'),
            array('广告编辑','mod,mod_'),
            array('广告删除','del'),
            array('静态生成','html,html_'),
            array('批量操作','bit_op'),
        );
    }
	public function __construct(){
		parent::__construct();
		$this->mid=0;
	}
	public function s(){
		return array('comment');
	}
	//全部评论
	public function index(){
	
	}
	//最新评论
	//http://www.reading.com/index.php/comment/comment?arc_id=7
	public function comment(){
		$mid=$this->mid;
		
		isset($_GET['page_id'])?'':$_GET['page_id']=1;
		$page_id=intval($_GET['page_id']);
		$page_id=max($page_id,1);
		
		$arc_id=intval($_GET['arc_id']);
		if($arc_id<=0) exit('arc_id error!');
		$data=$this->s_0->get_arc($arc_id);
		if(!$data || $data['status']==0) exit('data error!');
		
		
		$stat=$this->s_0->mystat($arc_id);
		
		$condition=array();
		$extra=" and `status`='1'";
		
		$condition['arc_id']=$arc_id;
		$extra.=" and `arc_id`='{$arc_id}'";
		
		$return=array();
		$return=$this->s_0->mylist($condition,$extra);
		
		$items=intval(isset($return[1])?$return[1]->total_num:0);
		
		include($this->viewer_dir.'comment/comment.php');
	}
	public function comment_list(){
		$mid=$this->mid;
		
		isset($_GET['page_id'])?'':$_GET['page_id']=1;
		$page_id=intval($_GET['page_id']);
		$page_id=max($page_id,1);
		
		$arc_id=intval($_GET['arc_id']);
		if($arc_id<=0) exit('arc_id error!');
		$data=$this->s_0->get_arc($arc_id);
		if(!$data || $data['status']==0) exit('data error!');
		
		
		$stat=$this->s_0->mystat($arc_id);
		
		$condition=array();
		$extra=" and `status`='1'";
		
		$condition['arc_id']=$arc_id;
		$extra.=" and `arc_id`='{$arc_id}'";
		
		$return=array();
		$return=$this->s_0->mylist($condition,$extra);
		
		$items=intval(isset($return[1])?$return[1]->total_num:0);
		
		$widget=load_service('a_widget',P_SVC);
		
		include($this->viewer_dir.'comment/comment_list.php');
	}
	//提交评论后的操作
	public function comment_(){
		if($_POST){
			//验证码
			$captcha=$_POST['captcha'];
			if($captcha!=$_SESSION['code']){
				alert('验证码不正确！');
				back();
			}
			
			$arc_id=intval($_POST['arc_id']);
			$data=$this->s_0->get_arc($arc_id);
			if(!$data || $data['status']==0) exit('data error!');
			
            if(!$data['allowcomment']){
                alert('本文不允许评论');
                back();
            }
            
			$content=$_POST['content'];
			$r=$this->s_0->is_allow_comment($content);
			
			if($r>0){
				$_POST['arc_title']=addslashes($data['title']);
				$_POST['arc_username']=addslashes($data['add_man']);
				$return=$this->s_0->add_($_POST);
				if($return[0]){
					alert('评论已提交，请等待审核！');
				}else{
					alert($return[1]);
				}
				back();
			}else{
				switch($r){
					case -1:
					alert('回复次数超过每日ip限评！');
					break;
					case -2:
					alert('回复间隔太短，请待会再评论！');
					break;
					case -3:
					alert('字数太少！');
					break;
					case -4:
					alert('字数太多！');
					break;
				}
				back();
			}
			
		}
		exit('!!!');
	}
	//响应异步支持反对
	public function ajax_vote(){
		$data=array('code'=>1,'msg'=>'');
		$mid=intval($_POST['mid']);
		$arc_id=intval($_POST['arc_id']);
		$cid=intval($_POST['cid']);
		$op=$_POST['op']?1:0;
		if($cid<=0){
			$data['code']=-1;
			$data['msg']='cid error!';
			echo json_encode($data);
			exit;
		}
		if($arc_id<=0){
			$data['code']=-2;
			$data['msg']='arc_id error!';
			echo json_encode($data);
			exit;
		}
		if(!in_array($mid,array(0))){
			$data['code']=-3;
			$data['msg']='mid error!';
			echo json_encode($data);
			exit;
		}
		if(get_cookie('comment_vote_'.$mid.'_'.$arc_id.'_'.$cid)){
			$data['code']=-4;
			$data['msg']='您已经对此评论表过态了';
			echo json_encode($data);
			exit;
		}
		$this->s_0->up_vote($cid,$op);
		set_cookie('comment_vote_'.$mid.'_'.$arc_id.'_'.$cid, 1,time()+365*86400);
		echo json_encode($data);
		exit;
	}
	
//
}